import AudioManager from '../../Common/AudioManager';
import { Cubekind } from './Cubekind';
import JackCube from './JackCube';
import GameCtr from '../../Controller/GameCtr';
import GameSelectManager from './GameSelectManager';
import GameData from '../../Common/GameData';
import nextJackCube from './nextJackCube';
import Http from '../../Common/Http';
import SoundManager from '../../Common/SoundManager';
import { PlayerStorage } from '../../Common/PlayerStorageManager';
import GameDataInstance from '../../Common/GameDataInstance';
import vv from '../../Common/vv';
const { ccclass, property } = cc._decorator;

//1,榔头 2双倍 3 激光 4刷新
enum TOOL_Type {
    toolHammer = 0,//1,榔头
    toolDouble = 1,//2双倍
    toolLaser = 2,//3 激光
    resurgence = 3,//4刷新
};

@ccclass
export class Game extends cc.Component {
    @property(cc.Node)
    SelectRingManager: cc.Node = null;//下方选择管理

    @property(cc.Node)
    ndCanvas: cc.Node = null;//下方选择管理

    @property(cc.Node)
    GameCubeManager: cc.Node = null;//游戏中的ring

    @property(cc.RichText)
    scroelabel: cc.RichText = null;//积分

    @property(cc.Node)
    ndteaching: cc.Node = null;//游戏中教学

    @property(cc.Label)
    resurgencelabel: cc.Label = null;//刷新次数label

    @property(cc.Label)
    GoldCountlabel: cc.Label = null;//金币

    @property(cc.Node)
    ndcanvas: cc.Node = null;

    @property(cc.Label)
    ndtiplabel: cc.Label = null;//

    @property(cc.Node)
    ndrevive: cc.Node = null;

    @property(cc.Node)
    ndcurlevel: cc.Node = null;//

    @property(cc.Node)
    ndlastlevel: cc.Node = null;//

    @property(cc.Node)
    ndprogress: cc.Node = null;//

    @property(cc.Node)
    ndlastJackcbue: cc.Node = null;//

    @property(cc.Sprite)
    sprFreindRankScroll: cc.Sprite = null;

    @property(cc.Node)
    ndSlider: cc.Node = null;//

    private tex = null;

    public isTouch = false;
    public touchID = 0;
    betStartPos = null;
    allScroe = 0;//总分数
    otherRankData = null;//超越对象
    otherIndex = -1;//超越对象第几个
    rankAdindex = -1;//超越对象第几个
    Allcanresurgence = 10;//一共刷新次数
    resurgence = 5;//刷新次数
    ToolDataList = [1, 1, 1, 1];//工具数量
    toolDouble = 3;
    public istoolHammer = false;//榔头
    lastpresentTime = 9;//榔头
    continuousErasureTime = 0;//连续消除
    gameLastTime = 60;
    isCanEliminatebool = false;

    tempscore = 0;
    canSelectCubesPos = null;
    NocanSelectCubesPos = null;
    isshowtex = false;
    public nextJackcubeId = [];
    onLoad() {
        GameCtr.getInstance().setGame(this);
        //GameCtr.deleteScore();
        this.setGameRingsid();
        //this.ndteaching.active = GameCtr.iSteaching;
        //AudioManager.getInstance().playMusic("audio/backmusic");
        GameCtr.isover = false;
        GameCtr.RoomId = Math.random().toString(32).substr(2);
        // WXCtr.setBannerAd(WXCtr.advid);
        // this.getCanSelectPos();
        // if (GameCtr.iSteaching) {
        //     this.refeshSelectCubesTeaching();
        // }
        // else
        // let innerAudioContext = tt.createInnerAudioContext()
        // innerAudioContext.src = 'resources/Audio/backmusic.mp3'
        // innerAudioContext.autoplay = true
        // innerAudioContext.loop = true
        // innerAudioContext.obeyMuteSwitch = true
        // innerAudioContext.  volume = 1
        // console.log('获取音频', innerAudioContext)
       // innerAudioContext.play()

        //innerAudioContext.string = 'resources/Audio/backmusic'
       // innerAudioContext.play()

        if(GameDataInstance.Instance.voiceon)
        {
            SoundManager.Instance.SetStorage(PlayerStorage.MusicOpen,true)
            SoundManager.Instance.SetStorage(PlayerStorage.EffectOpen,true)
        }
        else
        {
            SoundManager.Instance.SetStorage(PlayerStorage.MusicOpen,false)
            SoundManager.Instance.SetStorage(PlayerStorage.EffectOpen,false)
        }
        SoundManager.Instance.PlayMusic('backmusic')
        console.log('开始播放背景音乐')
       // this.AudioManager.children[0].getComponent(cc.AudioSource).play()
        this.gameFrist();
        this.getGamelevel();

       // this.schedule(() => {
            // this.isshowtex = true;
            //  WXCtr.submitScoreToWxComparetFriend(GameCtr.score);

       // }, 15);

        if (GameData.level <= 1) {
            this.ndteaching.active = true;
        }
        else {

        }
        this.openclose();
        this.setScore();
        //  this.showGameSlide();
    }

    openclose() {

        Http.send({
            url: Http.UrlConfig.GET_KAIGUAN,
            success: (resp) => {
                console.log("openclose", resp.data);
                if (resp.data.kg == 1) {
                    this.ndSlider.active = true;
                }
                else {
                    this.ndSlider.active = false;
                }
            },
            data: {
                zdid: "186"
            }
        });
    }

    closeteacting() {
        this.ndteaching.active = false;
    }

    onDestroy() {
        this.unscheduleAllCallbacks();
    }

    start() {
        if(window['wx'])
        {
            GameDataInstance.Instance.bannerAd.hide()
        }

      //  vv.share()


        if(GameData.level>=0&&GameData.level<6)
        {
            GameDataInstance.Instance.tomecount = 10
        }

        if(GameData.level>=6&&GameData.level<11)
        {
            GameDataInstance.Instance.tomecount = 9
        }

        if(GameData.level>=11&&GameData.level<16)
        {
            GameDataInstance.Instance.tomecount = 8
        }
        if(GameData.level>=16&&GameData.level<21)
        {
            GameDataInstance.Instance.tomecount = 7
        }
        if(GameData.level>=21&&GameData.level<26)
        {
            GameDataInstance.Instance.tomecount = 6
        }

       if(GameData.level>=26)
       {
        GameDataInstance.Instance.tomecount = 5
       }

        // GameCtr.rankingEntrance = "Start";//进入游戏后把排行榜的入口信息ref恢复成默认
        // if (window.wx != undefined) {
        //     this.tex = new cc.Texture2D();
        //     window.sharedCanvas.width = 1080;
        //     window.sharedCanvas.height = 1920;
        // }
    }

    setGameRingsid() {
        for (let index = 0; index < this.GameCubeManager.childrenCount; index++) {
            let element = this.GameCubeManager.children[index];
            let elementcom = element.getComponent(JackCube);
            if (cc.isValid(elementcom) && index != this.GameCubeManager.childrenCount - 1) {
                elementcom.resetCube();
                elementcom.CubeId = index;
                elementcom.setGameRoom(this)
            }
        }
        if (GameCtr.DataGame.length > 0) {
            for (let index = 0; index < 6; index++) {
                let nodec = this.GameCubeManager.children[index];
                let nodeccom = nodec.getComponent(JackCube);
                nodeccom.addCube(GameCtr.DataGame[index]);
            }
        }
        GameCtr.DataGame = [];
    }

    getGamelevel() {

        if(GameData.level>=10&&GameDataInstance.Instance.onkuaimusic == false)
        {
            SoundManager.Instance.PlayMusic('bgkuai')
            GameDataInstance.Instance.onkuaimusic = true
        }
        this.ndcurlevel.getChildByName('curlevel').getComponent(cc.RichText).string = GameData.level + "";
        this.ndlastlevel.getChildByName('curlevel').getComponent(cc.RichText).string = ((Number)(GameData.level) + 1) + "";

        let allexp = GameData.level * 10 * 0.4 + GameData.level + 10;
        let curexp = GameCtr.exp;


        console.log('当前的经验',curexp)
        console.log('目标的经验',allexp)
        this.ndprogress.getComponent(cc.ProgressBar).progress = curexp / allexp;
        if (allexp <= curexp) {
            this.setGamelevel();
            this.onceGame();
            SoundManager.Instance.PlayEffect("levelcomplete");
            let feinode = new cc.Node();
            let ndaLabel = feinode.addComponent(cc.Label);
            ndaLabel.fontSize = 180;
            ndaLabel.lineHeight = 210;
            this.ndcanvas.addChild(feinode);
            feinode.setPosition(80, 0);
            feinode.getComponent(cc.Label).string = "升级！" + "";

            feinode.runAction(cc.sequence(
                cc.moveTo(1, cc.v2(feinode.getPositionX(), feinode.getPositionY() + 100)),
                cc.fadeOut(1.0),
                cc.callFunc(() => {
                    this.ndcanvas.removeChild(feinode)
                })
            ));
        }
    }

    onceGame() {
        this.setGameRingsid();
        GameCtr.exp = 0;
        this.gameFrist();
        this.getGamelevel();
        GameData.score = GameCtr.score;
    }

    setGamelevel() {
        GameData.level = GameData.level + 1;
        GameData.score = GameCtr.score;
        //.setItem('gamelevel',GameData.level)
        console.log('升级关卡')
    }

    ReviveGameSetting() {
        let Cubekindcom = this.getComponent(Cubekind);
        for (let index = 0; index < 3; index++) {
            let element = Cubekindcom.arrRow[index];
            for (let index = 0; index < element.length; index++) {
                let cubeid = element[index];
                let cube = this.GameCubeManager.children[cubeid];
                let jcube = cube.getComponent(JackCube);
                jcube.resetCube();
                jcube.playAm();
            }
        }
    }


    //随机0到10
    randomNumberZHROToTEN(num) {
        let arrSize = [];
        for (let k = 0; k < num; k++) {//随机3个
            let id = Math.floor(Math.random() * 10) % 10;
            if (arrSize.indexOf(id) === -1) {
                arrSize.push(id);
            } else {
                k = k - 1;
                console.log("2");
                continue;
            }
        }
        return arrSize;
    }

    randomNumber(num) {
        let random: number = Math.random();
        console.log(random);
        if (random < num)
            return parseInt((random * 100) + "");
        else
            return this.randomNumber(num);
    }

    // //按钮点击事件
    // clickBtn(event, data) {
    //     switch (data) {
    //         case "showTrash":
    //             //this.showTrash(true);
    //             break;
    //         case "lockAdclearTrash":
    //             let call: Function = () => {
    //                 //   this.showTrash(false);
    //             }
    //             let fail: Function = () => {
    //                 //this.buyTip("视频没有播放完不能清除");
    //             }
    //             //this.ndTrash.getComponent(Trash).clearRubbishNum(call.bind(this), fail.bind(this));
    //             break;
    //         case "closeTrash":
    //             //    this.showTrash(false);
    //             break;

    //         case "revive":
    //             // if (GameCtr.surplusReviveTimes >= 0) {
    //             {
    //                 let call: Function = () => {
    //                     this.scheduleOnce(() => {
    //                         //this.ndtiplabel.node.active = false;
    //                     }, 2);
    //                     // this.ndtiplabel.node.active = true;
    //                     // this.ndtiplabel.string = "分享失败！";
    //                     console.log("分享失败！");
    //                 }
    //                 let completecall: Function = () => {
    //                     this.showRevive(false);
    //                     this.ReviveGameSetting();
    //                     GameCtr.surplusReviveTimes--;
    //                 }
    //                 console.log("WXCtr.share(WXCtr.share(WXCtr.share(");
    //                 WXCtr.share(true, completecall.bind(this), call.bind(this));
    //             }
    //             break;
    //         case "reviveshiping":
    //             {
    //                 let call: Function = (res) => {
    //                     if (res.isEnded) {
    //                         this.showRevive(false)
    //                         this.ReviveGameSetting();
    //                         //this.buyTip("复活成功");
    //                         GameCtr.surplusReviveTimes--;
    //                         //GameCtr.revive();
    //                     }
    //                     else {
    //                         //this.buyTip("视频没有播放完不能复活");
    //                     }
    //                     WXCtr.restoreVideoAdOnClose(call.bind(this));
    //                 }
    //                 WXCtr.showVideoAd(call.bind(this));
    //             }
    //             break;
    //         case "Closerevive":
    //             GameCtr.gameReviveData = [];
    //             // GameCtr.gameRevivetool = [];
    //             // GameCtr.ToolDataListTemp = [];
    //             this.showRevive(false)
    //             this.gameOver();
    //             break;
    //         case "yingfuhuobi":

    //             break;
    //         case "btnGamesliderD":
    //             this.btnGamesliderD();
    //             break;

            // case "playDrawReward":
            //     {
            //         let call: Function = (res) => {
            //             if (res.isEnded) {
            //                 let id = Math.floor(Math.random() * 10) % 8;
            //                 this.readyRotate(id);
            //                 this.playDrawReward();
            //                 this.ndbaoxiang.opacity = 155;
            //                 this.ndbaoxiang.getComponent(cc.Animation).stop("baoxiang");// ("baoxiang");
            //                 let call: Function = () => {
            //                     this.ndbaoxiang.opacity = 255;
            //                     this.ndbaoxiang.getComponent(cc.Animation).play("baoxiang");
            //                 }
            //                 this.schedule(call.bind(this), 300);


            //             }
            //             else {
            //                 this.buyTip("视频没有播放完不能抽奖");
            //             }
            //             WXCtr.restoreVideoAdOnClose(call.bind(this));
            //         }
            //         WXCtr.showVideoAd(call.bind(this));
            //     }
            //     break;
            // case "closeopenReopen":
            //     this.showReward(false);
            //     break;
            // case "closeshowDrawReward":
            //     this.showDrawReward(false);
            //     break;
            // case "showDrawReward":
            //     this.showDrawReward(true);
            //     break;
            // case "refeshSelectCubesOncebyONclicked":
            //     this.refeshSelectCubesOncebyONclicked(this.GameSelectManagerbackcubeviewselect);
            //     break;
            // case "refeshSelectCubesOncebyONclickedclose":
            //     this.ndGameSelectManagerbackcubeview.active = false;
            //     this.GameSelectManagerbackcubeviewselect = -1;
            //     break;
            // case "closeopenReopen"
            // this.showReward(true);
            // break;
       // }

   // }

    显示复活节点
    showRevive(isshow) {
        if (isshow) {
            // if (GameCtr.reviveData != null && GameCtr.reviveData.background_switch && GameCtr.surplusReviveTimes > 0 && GameCtr.rankingEntrance == "Start") {
            this.ndrevive.active = isshow;
            this.ndrevive.getChildByName("scorelabel").getComponent(cc.Label).string = GameCtr.score + "";

            // }
            // else {
            //     this.gameOver();
            // }
        }
        else {
            this.ndrevive.active = isshow;
        }
    }

    //每次游戏开始的时候
    gameFrist() {
        // let gailu = 0.22;
        // if (GameCtr.score > 150) {
        //     gailu = 0.45;
        // }
        // for (var k = 0; k < 1; k++) {//只有3个ring
        //     let index = this.randomNumber(gailu);
        //     console.log("refeshSRings", index);
        //     if (index == 5 || index == 10 || index == 9 || index == 4 || index == 3 || index == 8
        //         || index.kindid == 30 || index.kindid == 31 || index.kindid == 32 || index.kindid == 33) {
        //         k--;
        //         continue;
        //     }
        //     let ringtemp = cc.instantiate(this.pselectCube[index]);
        //     if (cc.isValid(ringtemp)) {
        //         let ringtempcom = ringtemp.getComponent(SelectBube);
        //         ringtempcom.setGameRoom(this);
        //         ringtempcom.setCubeId(k);
        //         ringtempcom.setCubeIDs(Cubekind.arrkinds[index]);
        //         ringtempcom.setkindid(index);
        //         ringtempcom.setSpr();
        //         ringtempcom.setisfrist(true);
        //         ringtemp.setScale(1, 1);
        //         this.SelectRingManager.addChild(ringtemp);
        //         ringtemp.setPosition(this.canSelectCubesPos);
        //         this.ndFrist = ringtemp;
        //     }
        // }
        this.refeshSelectCubes();
    }

    // // 刷新子域的纹理
    // _updateSubDomainCanvas() {
    //     if (window.sharedCanvas != undefined && this.tex != null && this.sprFreindRankScroll.node.active && this.isshowtex) {
    //         console.log("_updateSubDomainCanvas");
    //         this.tex.initWithElement(window.sharedCanvas);
    //         this.tex.handleLoadedTexture();
    //         this.sprFreindRankScroll.spriteFrame = new cc.SpriteFrame(this.tex);
    //         this.isshowtex = false;
    //     }
    // }


    /**
    *刷新 选择ring
    **/
    public refeshSelectCubes(nextarrid = null, fangshui = 0)//刷新 选择ring
    {
        if (nextarrid === null) {
            let num = 1;
            if (GameCtr.score > 15) {
                let id = Math.floor(Math.random() * 10) % 9;
                if (id > 8) {
                    num = 3;
                }
                else if (id > 7) {
                    num = 2;
                }
            }
            else {
                num = 1;
            }
            for (var k = 0; k < 1; k++) {//只有3个ring
                // let index = this.randomNumber(gailu);
                // if (fangshui > 0) {
                //     console.log("fangshui", fangshui);
                //     index = this.randomNumber(fangshui / 100);
                //     fangshui = 0;
                // }

                let id = Math.floor(Math.random() * 10) % 9;
                if (id >= 6) {
                    k = k - 1;
                    console.log("2");
                    continue;
                }
                let arrid = [];
                if (num == 3) {
                    arrid = [4, 5, 0];
                }
                else if (num == 2) {
                    arrid = [0, 1]
                }
                else {
                    arrid.push(id);
                }
                nextarrid = arrid;
            }

        }
        let ringtempcom = this.SelectRingManager.getComponent(GameSelectManager);
        ringtempcom.setGameRoom(this);
        ringtempcom.setSelectCubeId(nextarrid);
        ringtempcom.setSpr();
        this.refLastJcakCube();
        if (this.isGameOver()) {
            // this.SaveGameData();
            this.gameOver();
           //this.showRevive(true);
        }
        else {
        }
    }

    refLastJcakCube() {
        let num = 1;
        if (GameCtr.score > 15) {
            let id = Math.floor(Math.random() * 10) % 9;
            if (id > 8) {
                num = 3;
            }
            else if (id > 7) {
                num = 2;
            }
        }
        else {
            num = 1;
        }
        for (var k = 0; k < 1; k++) {//只有3个ring
            // let index = this.randomNumber(gailu);
            // if (fangshui > 0) {
            //     console.log("fangshui", fangshui);
            //     index = this.randomNumber(fangshui / 100);
            //     fangshui = 0;
            // }

            let id = Math.floor(Math.random() * 10) % 9;
            if (id >= 6) {
                k = k - 1;
                console.log("2");
                continue;
            }
            let arrid = [];
            if (num == 3) {
                arrid = [4, 5, 0];
            }
            else if (num == 2) {
                arrid = [0, 1]
            }
            else {
                arrid.push(id);
            }

            let AutoRefeshnum = Math.floor(Math.random() * 10) % 9;
            if (GameData.level < 10) {
                console.log("放水放水放水放水放水");
                arrid = this.AutoRefesh();
                let AutoRefeshnu = Math.floor(Math.random() * 10) % arrid.length;
                let temp = arrid[AutoRefeshnu];
                arrid = [];
                arrid.push(temp);
            }
            else if (GameData.level < 20) {
                if (AutoRefeshnum >= 3) {
                    console.log("放水放水放水放水放水11");
                    let arridtemp = this.AutoRefesh();
                    let AutoRefeshnuAutoRefeshnu = Math.floor(Math.random() * 10) % arrid.length;
                    let temp = arridtemp[AutoRefeshnuAutoRefeshnu];
                    arrid = [];
                    arrid.push(temp);

                }
            }
            else if (GameData.level < 30) {
                if (AutoRefeshnum >= 6) {
                    arrid = this.AutoRefesh();
                    let AutoRefeshnu = Math.floor(Math.random() * 10) % arrid.length;
                    let temp = arrid[AutoRefeshnu];
                    arrid = [];
                    arrid.push(temp);
                }
            }

            let ringtempcom = this.ndlastJackcbue.getComponent(nextJackCube);

            ringtempcom.resetCube();

            ringtempcom.addCube(arrid);

            this.nextJackcubeId = arrid;

        }
    }

    /**
    *判断游戏还可以 填的形状
    **/
    AutoRefesh() {
        let tempGameCubeID = [];
        for (let index01 = 0; index01 < 6; index01++) {
            let cube = this.GameCubeManager.children[index01];
            if (cc.isValid(cube)) {
                let cubeCom = cube.getComponent(JackCube);
                if (tempGameCubeID.length >= 6) {
                    break;
                }
                for (let index = 0; index < 6; index++) {
                    if (cubeCom.CubeSprid.indexOf(index) === -1 && tempGameCubeID.indexOf(index) === -1) {
                        tempGameCubeID.push(index);
                    }
                    else {

                    }
                }
            }
        }

        let SelectBubend = this.SelectRingManager.getComponent(GameSelectManager);
        for (let index = 0; index < tempGameCubeID.length; index++) {
            for (let index01 = 0; index01 < SelectBubend.selectCubeId.length; index01++) {
                let element = SelectBubend.selectCubeId[index];
                if (tempGameCubeID.indexOf(element) === -1) {

                }
                else {
                    let num = tempGameCubeID.indexOf(element);
                    tempGameCubeID.slice(num, 1);
                    console.log("sliceslice");
                }
            }
        }
        return tempGameCubeID;
    }


    //移动水果片
    Jackcubeid(id) {
        let ringtempcom = this.SelectRingManager.getComponent(GameSelectManager);
        ringtempcom.moveTopos(id);
    }

    sendJackArrid() {

    }

    colseteaching() {
        this.ndteaching.active = false;
    }

    /**
    * 教学
    */
    teaching() {
        if (GameCtr.iSteaching) {
            GameCtr.iSteaching = false;
            let Cubekindcom = this.getComponent(Cubekind);
            for (let index = 5; index < 7; index++) {
                // if (GameCtr.gameReviveData == null || GameCtr.gameReviveData.length == 0 || index >= Cubekindcom.arrRow.length) {
                //     break;
                // }
                let element = Cubekindcom.arrRow[index];
                for (let index = 0; index < element.length; index++) {
                    let cubeid = element[index];
                    let cube = this.GameCubeManager.children[cubeid];
                    let jcube = cube.getComponent(JackCube);
                    if (index == 6 || index == 5) {
                        jcube.addCube(-1);
                    }
                    else {
                        jcube.addCube(0);

                    }
                }
            }
            let posO = this.ndteaching.getPosition();
            let taPos = this.GameCubeManager.getPosition();
            this.ndteaching.runAction(cc.repeatForever(cc.sequence(
                cc.moveTo(1, cc.v2(posO.x, taPos.y - 100)),
                cc.fadeOut(1.0),
                cc.fadeIn(1.0),
                cc.moveTo(1, cc.v2(posO.x, posO.y - 100)))
            ));
        }
    }

    /**
    * 下面两个方法为测试用，自己根据实际需求处理
    */
    gameOver() {

        GameCtr.isover = true;
        //this.SaveGameData();
        GameCtr.gameOver();
    }

    /**
    *检查是否拖到game中的rings上
    **/
    public FkxxlbetJudge(cubeid, selectCubeId = null) {
        console.log('走了这里吗')
        // arrcubesID = this.removedouhao(arrcubesID);
        console.log("FkxxlbetJudge", cubeid, selectCubeId);
        for (let i = 0; i < this.GameCubeManager.childrenCount; i++) {
            let jackCube = this.GameCubeManager.children[cubeid];
            let jackCubeCom = jackCube.getComponent(JackCube); //转化为世界坐标

            let isCubeNull = true;
            for (let index = 0; index < selectCubeId.length; index++) {
                let element = selectCubeId[index];
                if (jackCubeCom.CubeSprid.indexOf(element) === -1) {

                } else {
                    return false;
                }
            }

            if (isCubeNull) {
                SoundManager.Instance.PlayEffect('down')

                jackCubeCom.addCube(selectCubeId);

                let max = jackCubeCom.isMax();
                if (max) {
                    console.log('哈哈哈哈哈哈max')
                    let fcube = jackCubeCom.CubeId - 1;
                    let scube = jackCubeCom.CubeId;
                    let rcube = jackCubeCom.CubeId + 1;
                    if (fcube < 0) {
                        fcube = 5;
                    }
                    if (rcube > 5) {
                        rcube = 0;
                    }
                    SoundManager.Instance.PlayEffect("extramove");
                    jackCubeCom = this.GameCubeManager.children[fcube].getComponent(JackCube);
                    this.AddScore(jackCubeCom.CubeSprid.length);
                    jackCubeCom.resetCube();
                    console.log('开始播放特效')
                    jackCubeCom.playAm();
                    console.log("jackCubeCom", jackCubeCom);
                    if (jackCubeCom.CubeSprid.length > 0) {
                        let feinode = new cc.Node();
                        let ndaLabel = feinode.addComponent(cc.Label);
                        ndaLabel.fontSize = 80;
                        ndaLabel.lineHeight = 85;
                        this.GameCubeManager.addChild(feinode);
                        feinode.setPosition(jackCubeCom.node.getPosition());
                        feinode.getComponent(cc.Label).string = jackCubeCom.CubeSprid.length + "";
                        feinode.runAction(cc.sequence(
                            cc.moveTo(feinode.getPositionX(), feinode.getPositionY() + 100),
                            cc.fadeOut(1.0),
                            cc.callFunc(() => {
                                console.log('哈哈哈哈哈哈')
                                this.GameCubeManager.removeChild(feinode)
                            })
                        ));
                    }


                    jackCubeCom = this.GameCubeManager.children[scube].getComponent(JackCube);
                    this.AddScore(jackCubeCom.CubeSprid.length);
                    jackCubeCom.resetCube();
                    jackCubeCom.playAm();
                    console.log("jackCubeCom1", jackCubeCom);
                    if (jackCubeCom.CubeSprid.length > 0) {
                        let feinode = new cc.Node();
                        let ndaLabel = feinode.addComponent(cc.Label);
                        ndaLabel.fontSize = 80;
                        ndaLabel.lineHeight = 85;
                        this.GameCubeManager.addChild(feinode);
                        feinode.setPosition(jackCubeCom.node.getPosition());
                        feinode.getComponent(cc.Label).string = jackCubeCom.CubeSprid.length + "";
                        feinode.runAction(cc.sequence(
                            cc.moveTo(feinode.getPositionX(), feinode.getPositionY() + 100),
                            cc.fadeOut(1.0),
                            cc.callFunc(() => {
                                this.GameCubeManager.removeChild(feinode)
                            })
                        ));
                    }
                    jackCubeCom = this.GameCubeManager.children[rcube].getComponent(JackCube);
                    this.AddScore(jackCubeCom.CubeSprid.length);
                    jackCubeCom.resetCube();
                    jackCubeCom.playAm();
                    console.log("jackCubeCom2", jackCubeCom);
                    if (jackCubeCom.CubeSprid.length > 0) {
                        let feinode = new cc.Node();
                        let ndaLabel = feinode.addComponent(cc.Label);
                        ndaLabel.fontSize = 80;
                        ndaLabel.lineHeight = 85;
                        this.GameCubeManager.addChild(feinode);
                        feinode.setPosition(jackCubeCom.node.getPosition());
                        feinode.getComponent(cc.Label).string = jackCubeCom.CubeSprid.length + "";
                        feinode.runAction(cc.sequence(
                            cc.moveTo(feinode.getPositionX(), feinode.getPositionY() + 100),
                            cc.fadeOut(1.0),
                            cc.callFunc(() => {
                                this.GameCubeManager.removeChild(feinode)
                            })
                        ));
                    }
                } else {
                    this.AddScore(selectCubeId.length);
                    if (selectCubeId.length > 0) {
                        let feinode = new cc.Node();
                        let ndaLabel = feinode.addComponent(cc.Label);
                        ndaLabel.fontSize = 80;
                        ndaLabel.lineHeight = 85;
                        this.GameCubeManager.addChild(feinode);
                        feinode.setPosition(jackCubeCom.node.getPosition());
                        feinode.getComponent(cc.Label).string = "+" + selectCubeId.length + "";
                        let ddd = feinode.getPositionY() + 800;
                        feinode.runAction(cc.sequence(
                            cc.moveTo(1, cc.v2(feinode.getPositionX(), feinode.getPositionY() + 100)),
                            cc.fadeOut(1.0),
                            cc.callFunc(() => {
                                this.GameCubeManager.removeChild(feinode)
                            })
                        ));
                    }

                }
               // console.log('开始播放放上去的音效')

            //    let InnerAudioContext = tt.createInnerAudioContext()
            //    InnerAudioContext.src = 'Audio/down'
            //    InnerAudioContext.autoplay = true
                return true;
            }
            else {
                return false;
            }

        }
        return false;
    }

    //备选移动到首选
    moveToFristSelect() {
        // let call: Function = () => {
        //     let ringtempcom = this.ndScend.getComponent(SelectBube);
        //     ringtempcom.setisfrist(true);
        //     this.ndFrist = this.ndScend;
        //     let fangshui = this.AutoRefesh();
        //     this.refeshSelectCubes(fangshui);
        // }

        // this.ndScend.runAction(
        //     cc.sequence(
        //         cc.moveTo(0.3, this.canSelectCubesPos),
        //         cc.callFunc(call.bind(this)),
        //     )
        // )
    }

    AddScore(num) {
        GameCtr.addScore(num);
        GameCtr.exp = GameCtr.exp + num;
        this.getGamelevel();
    }

    isCanEliminate(arrid) {
        let arrRow = [];
        let arrCol = [];
        let isEliminateonce = false;
        for (let index = 0; index < arrid.length; index++) {
            let element = arrid[index];
            let row = Math.floor(element / 10);
            let col = Math.floor(element % 10);
            if (arrRow.indexOf(row) === -1) {
                arrRow.push(row);
            }
            if (arrCol.indexOf(col) === -1) {
                arrCol.push(col);
            }
        }

        let arrRowids = [];
        let arrColids = [];
        let Cubekindcom = this.getComponent(Cubekind);
        for (let index = 0; index < arrRow.length; index++) {
            arrRowids.push(Cubekindcom.arrRow[arrRow[index]]);
        }

        for (let index = 0; index < arrCol.length; index++) {
            arrColids.push(Cubekindcom.arrCol[arrCol[index]]);
        }

        return isEliminateonce;
    }

    setScore() {
        cc.sys.localStorage.setItem('maxscore',GameCtr.score)
        this.scroelabel.string = GameCtr.score + "";

    }

    jackCubeSArrId(jackCubeid, arrcubes) {
        let temp = [];
        for (let index = 0; index < arrcubes.length; index++) {
            let element = arrcubes[index];
            temp.push(element + jackCubeid);
        }
        return temp;
    }

    removedouhao(arrcubesID) {
        let cubesID = [];
        for (let index = 0; index < arrcubesID.length; index++) {
            let element = arrcubesID[index];
            if (element === ",") {

            }
            else {
                cubesID.push(element);
            }
        }
        return cubesID;
    }

    /**
    *判断游戏是否结束
    **/
    isGameOver() {
        let SelectBubend = this.SelectRingManager.getComponent(GameSelectManager);
        for (let index01 = 0; index01 < 6; index01++) {
            let cube = this.GameCubeManager.children[index01];
            if (cc.isValid(cube)) {
                let cubeCom = cube.getComponent(JackCube);

                let isall = true;
                for (let index = 0; index < SelectBubend.selectCubeId.length; index++) {
                    let element = SelectBubend.selectCubeId[index];
                    if (cubeCom.CubeSprid.indexOf(element) == -1) {

                    }
                    else {
                        isall = false;
                    }
                }
                if (isall) {
                    return false;
                }
            }
        }
        return true;
    }


    removeRing(SelectCubeId) {
        // this.SelectRingManager.removeAllChildren();
        // if (this.SelectRingManager.childrenCount == 0) {
        //     this.SelectRingManager.removeAllChildren();
        //     this.refeshSelectCubes();
        // }
        // if (this.isGameOver()) {
        //     // this.SaveGameData();
        //     // this.Gameovervoer();
        //     this.showRevive(true);
        // }
        // else {
        // }
    }

    SaveGameData() {
        GameCtr.gameReviveData = [];
        for (let index = 0; index < this.GameCubeManager.childrenCount; index++) {
            let element = this.GameCubeManager.children[index];
            let elementCom = element.getComponent(JackCube);
            if (cc.isValid(elementCom)) {
                let data = {
                    cubeid: elementCom.CubeId,
                    sprid: elementCom.CubeSprid
                };
                GameCtr.gameReviveData.push(data);
            }
        }
        GameCtr.gameReviveData.push(GameCtr.score);
        console.log("setGameRingsid", GameCtr.gameReviveData);
    }

    Gameovervoer() {
        this.showRevive(true);
    }

    /**
    *跳转到主页
    */
    onHome() {
        GameCtr.DataGame = [];
        for (let index = 0; index < 6; index++) {
            let nodec = this.GameCubeManager.children[index];
            let nodeccom = nodec.getComponent(JackCube);
            GameCtr.DataGame.push(nodeccom.CubeSprid);
        }
        GameData.score = GameCtr.score;
        if(GameDataInstance.Instance.MaxScore<=GameCtr.score)
        {
            cc.sys.localStorage.setItem('maxscore', GameCtr.score)
        }
        GameCtr.showStart();
    }

    /**
    *随机 消除3个
    **/
    randomDeletNumber() {
        console.log("randomNumber");
        let arr1 = [];
        let arrSize = new Array();
        let arrColor = new Array();

        for (let i = 0; i < 9; i++) {
            arr1.push(i);
        }

        for (let k = 0; k < 3; k++) {//随机大小
            let id = Math.floor(Math.random() * 10) % 9;
            let comp = this.GameCubeManager.children[arr1[id]].getComponent(JackCube);

        }

        for (let index = 0; index < arrSize.length; index++) {
            let ringid = arrSize[index];
            let ringcom = this.GameCubeManager.children[ringid].getComponent(JackCube);

        }
        if (arrSize.length > 0) {
            SoundManager.Instance.PlayEffect("delete");
        }
    }

    closeSeek() {
        //  this.ndSeek.active = false;
    }


    // SeekShare() {
    //     let call = () => {
    //         GameCtr.addToolByTypeCtr(2);
    //         WXCtr.restoreVideoAdOnClose();
    //     }
    //     WXCtr.showVideoAd(call.bind(this));
    //     // let call = () => {
    //     //     this.scheduleOnce(() => {
    //     //         this.ndtiplabel.node.active = false;
    //     //     }, 3);
    //     //     this.ndtiplabel.node.active = true;
    //     //     this.ndtiplabel.string = "分享失败！";
    //     //     console.log("分享失败！");
    //     // }
    //     // WXCtr.shareHlep(null, call.bind(this));
    // }

    randomNumberSeek(num) {
        let random: number = Math.random();
        console.log(random);
        if (random < num)
            return true;
        else
            return false;
    }

    update(dt) {
        // this._updateSubDomainCanvas();
    }

    // //显示广告按钮
    // showGameSlide() {
    //     let spr = this.ndSlider.getComponent(cc.Sprite);
    //     console.log("showSlide", GameCtr.sliderData);
    //     GameCtr.loadImg(spr, GameCtr.sliderData[0].thumb);
    // }

    // /**
    // * 点击
    // */
    // btnGamesliderD() {
    //     console.log("GameCtr.otherData = ", GameCtr.sliderData);
    //     if (GameCtr.sliderData)
    //         WXCtr.gotoOther(GameCtr.sliderData[0]);
    // }

    updateGame() {
        // let temp = 60;
        // this.schedule(() => {
        //     temp--;
        //     if (temp < 0) {
        //         // this.ndpresent.active = false;
        //         GameCtr.gameOver();
        //         return;
        //     }
        //     let use = this.ndpresent.getChildByName("use");
        //     if (cc.isValid(use)) {
        //         let time = use.getChildByName("lasttime").getComponent(cc.Label);
        //         time.string = temp + "";
        //     }
        // }, 1, 60);
    }
}
